﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SS.HP.Model;

namespace SS.HP.Tests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            Series hp = new Series() {Name = "Harry Potter"};
            var book1 = new Book { SeriesIndex = 1, Series = hp, Title = "HP1", RecommendedRetailPrice = 10.0 };
            var book2 = new Book { SeriesIndex = 2, Series = hp, Title = "HP2", RecommendedRetailPrice = 10.0 };
            var book3 = new Book { SeriesIndex = 3, Series = hp, Title = "HP3", RecommendedRetailPrice = 10.0 };
            var book4 = new Book { SeriesIndex = 4, Series = hp, Title = "HP4", RecommendedRetailPrice = 10.0 };
            var book5 = new Book { SeriesIndex = 5, Series = hp, Title = "HP5", RecommendedRetailPrice = 10.0 };

            Cart cart = new Cart();
            cart.AddBook(book1);
            cart.AddBook(book2);
            cart.AddBook(book2);
            cart.AddBook(book3);
            cart.AddBook(book4);
            cart.AddBook(book5);

            IEnumerable<IGrouping<int, Book>> booksInSeries = cart.GetBooksInSeriesBySeriesIndex(hp);
            foreach (var bookNumber in booksInSeries.OrderBy(x=>x.Key))
            {
                Debug.Print(string.Format("{0} picked of book number {1}", bookNumber.Count(), bookNumber.Key));
            }
        }

        [TestMethod]
        public void TestMethod2()
        {
            Series hp = new Series() { Name = "Harry Potter" };
            var book1 = new Book { SeriesIndex = 1, Series = hp, Title = "HP1", RecommendedRetailPrice = 8.0 };
            var book2 = new Book { SeriesIndex = 2, Series = hp, Title = "HP2", RecommendedRetailPrice = 8.0 };
            var book3 = new Book { SeriesIndex = 3, Series = hp, Title = "HP3", RecommendedRetailPrice = 8.0 };
            var book4 = new Book { SeriesIndex = 4, Series = hp, Title = "HP4", RecommendedRetailPrice = 8.0 };
            var book5 = new Book { SeriesIndex = 5, Series = hp, Title = "HP5", RecommendedRetailPrice = 8.0 };

            Cart cart = new Cart();
            cart.AddBook(book1.DeepClone());
            cart.AddBook(book2.DeepClone());
            cart.AddBook(book2.DeepClone());
            cart.AddBook(book3.DeepClone());
            cart.AddBook(book4.DeepClone());
            cart.AddBook(book5.DeepClone());

            var expected = (8 * 5 * 1 * .75) + (8 * 1 * 1);

            var result = cart.Price;
            Assert.IsTrue(result > 0);
            const double epsilon = 0.001;
            Assert.IsTrue(Math.Abs(result - expected) < epsilon);


            cart = new Cart();
            cart.AddBook(book1.DeepClone());
            cart.AddBook(book1.DeepClone());
            cart.AddBook(book1.DeepClone());
            cart.AddBook(book2.DeepClone());
            cart.AddBook(book2.DeepClone());
            cart.AddBook(book2.DeepClone());
            cart.AddBook(book3.DeepClone());
            cart.AddBook(book3.DeepClone());
            cart.AddBook(book4.DeepClone());
            cart.AddBook(book4.DeepClone());
            cart.AddBook(book5.DeepClone());
            cart.AddBook(book5.DeepClone());

            expected = (8*5*2*.75) + (8*2*.95);
            result = cart.Price;
            Assert.IsTrue(result > 0);
            Assert.IsTrue(Math.Abs(result - expected) < epsilon);
        }

        [TestMethod]
        public void TestMethod3()
        {
            Series hp = new Series() { Name = "Harry Potter" };
            var book1 = new Book { SeriesIndex = 1, Series = hp, Title = "HP1", RecommendedRetailPrice = 8.0 };
            var book2 = new Book { SeriesIndex = 2, Series = hp, Title = "HP2", RecommendedRetailPrice = 8.0 };
            var book3 = new Book { SeriesIndex = 3, Series = hp, Title = "HP3", RecommendedRetailPrice = 8.0 };
            var book4 = new Book { SeriesIndex = 4, Series = hp, Title = "HP4", RecommendedRetailPrice = 8.0 };
            var book5 = new Book { SeriesIndex = 5, Series = hp, Title = "HP5", RecommendedRetailPrice = 8.0 };

            Cart cart = new Cart();
            cart.AddBook(book1.DeepClone());
            cart.AddBook(book1.DeepClone());
            cart.AddBook(book2.DeepClone());
            cart.AddBook(book2.DeepClone());
            cart.AddBook(book3.DeepClone());
            cart.AddBook(book3.DeepClone());
            cart.AddBook(book4.DeepClone());
            cart.AddBook(book5.DeepClone());

            var expected = (8 * 5 * 1 * .75) + (8 * 3 * .9);

            var result = cart.Price;
            Assert.IsTrue(result > 0);
            const double epsilon = 0.001;
            Assert.IsTrue(Math.Abs(result - expected) < epsilon);


        }
    }
}
